package com.x.erp.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.x.erp.model.CarAuth;
import com.x.framework.dao.BaseDao;

/**
 * 车辆认证DAO
 */
@Repository
public class CarAuthDao extends BaseDao {

	private final static String selectCarAuthSql = "SELECT " +
			"eca.* " +
			"FROM " +
			"ERP_CAR_AUTH eca "+
			"JOIN ERP_CAR ec on eca.CAR_ID = ec.CAR_ID " +
			"JOIN ERP_MARKET em ON eca.MARKET_ID = em.MARKET_ID " +
			"WHERE 1 = 1 ";

	/**
	 * 通过carAuth查询单个车辆认证信息
	 * 
	 * @param carAuth
	 * @return
	 * @throws Exception
	 */
	public CarAuth selectCarAuth(CarAuth carAuth) throws Exception {
		StringBuilder sql = new StringBuilder(selectCarAuthSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, carAuth);
		List<CarAuth> results = this.queryForList(sql.toString(), CarAuth.class, params);
		if (results != null && results.size() > 0) {
			return results.get(0);
		}
		return null;
	}
	/**
	 * 拼接查询条件
	 * @param sql
	 * @param params
	 * @param carAuth
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, CarAuth carAuth) {
		if (carAuth != null) {
			if (this.isString(carAuth.getAuthId())) {
				sql.append("AND eca.AUTH_ID = ? ");
				params.add(carAuth.getAuthId());
			}
			if (this.isString(carAuth.getAuthNo())) {
				sql.append("AND eca.AUTH_NO = ? ");
				params.add(carAuth.getAuthNo());
			}
			if (this.isString(carAuth.getAuthCompany())) {
				sql.append("AND eca.AUTH_COMPANY = ? ");
				params.add(carAuth.getAuthCompany());
			}
			if (this.isString(carAuth.getDetectionId())) {
				sql.append("AND eca.DETECTION_ID = ? ");
				params.add(carAuth.getDetectionId());
			}
			if (this.isString(carAuth.getMarketId())) {
				sql.append("AND eca.MARKET_ID = ? ");
				params.add(carAuth.getMarketId());
			}
			if (this.isString(carAuth.getShopId())) {
				sql.append("AND eca.SHOP_ID = ? ");
				params.add(carAuth.getShopId());
			}
			if (this.isString(carAuth.getCarId())) {
				sql.append("AND eca.CAR_ID = ? ");
				params.add(carAuth.getCarId());
			}
			if (this.isString(carAuth.getCreateUserId())) {
				sql.append("AND eca.CREATE_USER_ID = ? ");
				params.add(carAuth.getCreateUserId());
			}
		}
	}

	private final static String selectCurrentNumSql = "SELECT " +
			"COUNT(eca.AUTH_ID) + 1 " +
			"FROM " +
			"ERP_CAR_AUTH eca " +
			"JOIN ERP_CAR ec on eca.CAR_ID = ec.CAR_ID " +
			"JOIN ERP_MARKET em ON eca.MARKET_ID = em.MARKET_ID " +
			"WHERE em.MARKET_ID = ? AND eca.AUTH_NO LIKE ? ";

	/**
	 * 通过marketID查询当前应该录入的认证证书编号序号（当天总数+1）
	 * 
	 * @param carAuth
	 * @return
	 * @throws Exception
	 */
	public int selectCurrentNum(CarAuth carAuth, String authCode) {
		StringBuilder sql = new StringBuilder(selectCurrentNumSql);
		List<Object> params = new ArrayList<Object>();
		params.add(carAuth.getMarketId());
		params.add(authCode);
		return this.queryForInt(sql.toString(), params);
	}
}
